<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

      
class facture_ctr extends CI_Controller {
	function __construct()
	{
		parent::__construct();
		$this->load->library("pagination");
		$this->load->helper(array('form', 'url'));
		$this->load->library('image_lib');
		$this->load->helper("file");
        $this->load->helper('form');
		$this->load->library('Convert_ctr');// elle convert les chiffre en lettre arab
	}

	function index()
	{
		
		$this->load->model('fournisseur_mdl'); // load the file welcome_mdl.php
		$this->load->model('engagement_mdl'); // load the file welcome_mdl.php
		$data1['records'] = $this->fournisseur_mdl->get_all(); // 
		$data2['records2'] = $this->engagement_mdl->get_all_recherche(); // 
        $data3['four']=$data1;
		$data3['enga']=$data2;
       // $this->facture_img_mdl->get_facture();
	   
	   $this->load->model('facture_mdl');
		$this->load->model('charticle_mdl');
		$this->load->model('fournisseur_mdl');
		
		$data4['rec'] = $this->facture_mdl->get_details(1); // get  one fournisseur per id
		//$id_fournisseur1=$data1['rec'];
		//$id_fournisseur=$id_fournisseur1[0]->id_fournisseur_fk;


		$data5['rec'] = $this->fournisseur_mdl->get_all();   // get all  for autocomplete
		$data6['rec'] = $this->charticle_mdl->get_all();  //get all activity list for autocomplete 

		$data3['fournisseur']    = $data4;
		$data3['liste_fourn']    = $data5;
		$data3['liste_article'] = $data6;
		
		$this->load->view('facture_upload_vew',$data3);
		
	}
	
	function index_test()
	{
		 $this->load->library('session');
		$this->load->model('charticle_mdl'); // load the file welcome_mdl.php
		$this->load->model('facture_mdl'); // load the file welcome_mdl.php
		$charticle = $this->charticle_mdl->get_all(); // call the method filtera inside the class welcome_mdl
		
		$data_charticle = array('data_charticle'=>$charticle); //ajouter la table r1 a data		
		$this->session->set_userdata($data_charticle); // mettre $data_charticle dans une var globale
		
		 $config = array();
          $config["base_url"] = base_url( "facture_ctr/index_test");
		  $config["total_rows"] =$this->facture_mdl->record_count_all();
		  $config["per_page"] = 20;
          $config["uri_segment"] = 3;
		  $config['first_link'] = 'PREMIER';
		  $config['last_link'] = 'DERNIER';
 

        $this->pagination->initialize($config);
		 $page = ($this->uri->segment(3)) ? $this->uri->segment(3) : 0;
		  $data["titre"] = 'قائمة الفواتير';
       $data["results"] = $this->facture_mdl->get_all($config["per_page"], $page);
	   $data["results_recherche"] = $this->facture_mdl->get_all_nom();
        $data["links"] = $this->pagination->create_links();
		  $this->load->view('facture_vew',$data);  // send the result from the model to the view 
	
	}
    
	public function get_facture_per_article2($x1)
    {
		$this->load->model('facture_mdl'); // load the file welcome_mdl.php
	 $this->load->library('session');
	  $tab['val']=$x1;
	   $tab['nbr-enreg']= $this->facture_mdl->record_count_art($x1);
	   $tab['premier_appelle']=TRUE;
	  $this->session->set_userdata($tab); // mettre $tab dans une var globale
	  
	  

		
		$this->get_facture_per_article3();
		
	
	
    }
	
	public function get_facture_per_article3()
    {
		
	 $this->load->library('session');
        $x1=$this->session->userdata('val'); //access la var globale val

		$this->load->model('facture_mdl'); // load the file welcome_mdl.php
		$this->load->model('charticle_mdl'); // load the file welcome_mdl.php
			 /////pagination
		   $config = array();
         $config["base_url"] = base_url("facture_ctr/get_facture_per_article3");
		  $config["total_rows"] =$this->session->userdata('nbr_enreg');
		  $config["per_page"] = 20;
          $config["uri_segment"] = 3;
		 
		  
 //
       $this->pagination->initialize($config);
	   
	 $premier=$this->session->userdata('premier_appelle'); //access la var globale val
	 if($premier){
	 	$page=0;
		 $tab['premier_appelle']=FALSE;
		   $this->session->set_userdata($tab); // mettre $tab dans une var globale
		   }
	else $page = ($this->uri->segment(3)) ? $this->uri->segment(3) : 0;
		
       $data["results"] = $this->facture_mdl->get_per_article($config["per_page"], $page ,$x1);
	  //  $data["titre"] = 'قائمة الفواتير الخاصة بالمادة :'.$x1;
	   $data["results_recherche"] = $this->facture_mdl->get_all_per_article($x1);
        $data["links"] = $this->pagination->create_links();
		
			$tt=$data["results"];
		if($tt){
			$nom_article=$tt[0]->nom_art;
			$data['titre']='فواتير  الخاصة بالمادة '.$nom_article;
		}else{
			$art = $this->charticle_mdl->get_nom_article($x1);
			$data['titre']=': لا يوجد فواتير خاصة بالمادة  <br>'.$art[0]->nom ;
		}
		 $this->load->view("facture_vew",$data);	
		
		
		
	
	
    }
  
    public function find_name()
    {
		$this->load->model('facture_mdl'); // load the file welcome_mdl.php
		$v=$this->input->post('nom');
		
		 $config = array();
         $config["base_url"] = base_url("facture_ctr/find_name");
		  $config["total_rows"] =$this->session->userdata('nbr_enreg');
		  $config["per_page"] = 20;
          $config["uri_segment"] = 3;
		  
		  $this->pagination->initialize($config);
	   
	
	
	 	$page=0;
		 $tab['premier_appelle']=FALSE;
		   $this->session->set_userdata($tab); // mettre $tab dans une var globale
		
	 $page = ($this->uri->segment(3)) ? $this->uri->segment(3) : 0;
		
       $data["results"] = $this->facture_mdl->get_recherche($config["per_page"], $page ,$v);
	   $data["titre"] = ':  قائمة الفواتير الخاصة بالمورد <br>'.$v.'</rb>';
	   $data["results_recherche"] = $this->facture_mdl->get_all_nom();
        $data["links"] = $this->pagination->create_links();
		
		 $this->load->view("facture_vew",$data);	
		  
		}
		
	public function detail ($facture_id){
		
		$this->load->model('facture_mdl');
		$this->load->model('charticle_mdl');
		$this->load->model('fournisseur_mdl2');
		
		$data1 = $this->facture_mdl->get_details($facture_id); // get  one fournisseur per id
		//$id_fournisseur1=$data1['rec'];
		//$id_fournisseur=$id_fournisseur1[0]->id_fournisseur_fk;
        $mont=$data1[0]->montant_fact;
        $lettre=$this->convert_ctr->convert_number($mont);
		$data2 = $this->fournisseur_mdl2->get_all();   // get all  for autocomplete
		$data3 = $this->charticle_mdl->get_all();  //get all activity list for autocomplete 

		$r['fournisseur']    = $data1;
		$r['liste_fourn']    = $data2;
		$r['liste_article'] = $data3;
		$r['lettre'] = $lettre;
	
		$this->load->view('facture_details_vew',$r); 
	}
	
		public function update ($id_fact)
	{
		
		///////////////////////	
		$nom_fournisseur= $this->input->post('fournisseur');
		$num_art= $this->input->post('article');
		$montant= $this->input->post('montant');
		$mondat= $this->input->post('mondat');
		$numero= $this->input->post('numero'); 
	    $image=$this->input->post('image');
		$table['art']=$num_art;
		$table['montant']=$montant;
		
		
		// chargement de image facture
	    $image_path='C:\Ampps\www\iqama_04_05_14\assets\telechargement';
	    //$image_path = "http://www.iqama3000.com/iqama_04_05_14/assets/telechargement";
		$config['upload_path'] = $image_path;
	    $config['file_name']=$id_fact;
		$config['allowed_types'] = 'gif|jpg|png';
		//$config['encrypt_name'] = FALSE;
		$config['overwrite'] = TRUE;
		$this->load->library('upload', $config);
		$config['image_library'] = 'gd2';
	
		if ( ! $this->upload->do_upload())
		{
			$error = array('error' => $this->upload->display_errors());
			
			
         //  $this->detail($id_fact);
			//$this->load->view('facture_upload_vew', $error);
		}
		else
		{
			 
			$data = array('upload_data' => $this->upload->data());
	    
			$fpath=$image_path.'\\\\'.$data['upload_data']['file_name'];
			
		   $config['image_library'] = 'gd2';
		   $config['source_image'] = $fpath;
			$config['create_thumb'] = FALSE;
            $config['maintain_ratio'] = TRUE;
  		
			   $config['width'] = 200;
	           $config['height'] = 200;
		 
			    $this->load->library('image_lib', $config);
			    $this->image_lib->clear();
                $this->image_lib->initialize($config);
                $this->image_lib->resize();
				
				 
			
	
		$image=$config['file_name'].$data['upload_data']['file_ext'];
		
	
	    		
		
		
		}
		$this->load->model('facture_mdl'); 
		$this->facture_mdl->get_update($id_fact,$nom_fournisseur,$num_art,$montant,$mondat,$numero,$table,$image);
		 $this->load->view('facture_update_success_vew');
	}

     public function supprimer_facture ($id_fact)
	{

		$this->load->model('facture_mdl'); 
		$this->load->model('mondat_mdl'); 
		
		$list_pieces=$this->facture_mdl->get_bc_conv_liee($id_fact);
		if($list_pieces[0]->id_bc){
			$this->facture_mdl->annule_facture_bc($list_pieces[0]->id_bc);
		}
		if($list_pieces[0]->id_convetion){
			
		}
		
		$this->facture_mdl->supprime_fact($id_fact);
		//if($id_mondat) $this->mondat_mdl->supprime_mondat($id_mondat);
		
		
		 $this->load->view('facture_update_success_vew');
	}
	
	
		public function payer_facture($id_fact)
	{

		$num_cheque= $this->input->post('num_cheque');

		//$this->load->model('bc_mdl'); // load 
		$this->load->model('mondat_mdl'); // load 
		$this->load->model('facture_mdl'); // load 
		$this->load->model('convention_mdl'); // load 
		
		
		$today = getdate();
		$date=$today['year'].'-'.$today['mon'].'-'.$today['mday'];
		
		
		// on test si numero de bc exite
		$fact_detail=$this->facture_mdl->get_details($id_fact);
		$id_fourn=$fact_detail[0]->id_fournisseur_fk;
		
		$num_mond=$this->mondat_mdl->get_num_mondat();
		if($num_mond){
			$numero_mondat=$num_mond[0]->numero+1;
		}else{
			$numero_mondat=1;
		}
		$montant_facture=$fact_detail[0]->montant_fact;
		$mondat=$this->mondat_mdl->ajout_new_mondat($num_cheque,$montant_facture,$date,$numero_mondat);
		$id_mondats=$mondat[0]->id_mandat;
		$this->facture_mdl->attacher_mondat_facture($id_mondats,$id_fact);
		//$this->bc_mdl->attacher_bc_mondat($id_bc,$id_mondats);
	
		$this->detail($id_fact);
	}
	
	public function payer($id_fact)
	{
		
		///////////////////////
		//$nature= $this->input->post('nature');
		$num_cheque= $this->input->post('num_cheque');
		//$numero_piece= $this->input->post('num'); 
		
		
		$this->load->model('bc_mdl'); // load 
		$this->load->model('mondat_mdl'); // load 
		$this->load->model('facture_mdl'); // load 
		$this->load->model('convention_mdl'); // load 
		
		
		$today = getdate();
		$date=$today['year'].'-'.$today['mon'].'-'.$today['mday'];
		
		
		// on test si numero de bc exite
		$fact_detail=$this->facture_mdl->get_details($id_fact);
		$id_fourn=$fact_detail[0]->id_fournisseur_fk;
		
		if($nature=='bc'){// si le document est un bc
			
		$bc_existe=$this->bc_mdl->existe_bc($numero_piece,$id_fourn);
		
		if($bc_existe){
			$id_bc=$bc_existe[0]->id_bc;
			
			$num_mond=$this->mondat_mdl->get_num_mondat();
		if($num_mond){
			$numero_mondat=$num_mond[0]->numero+1;
		}else{
			$numero_mondat=1;
		}
		$montant_facture=$fact_detail[0]->montant_fact;
		$mondat=$this->mondat_mdl->ajout_new_mondat($num_cheque,$montant_facture,$date,$numero_mondat);
		$id_mondats=$mondat[0]->id_mandat;
		$this->facture_mdl->attacher_mondat_facture($id_mondats,$id_fact);
		$this->bc_mdl->attacher_bc_mondat($id_bc,$id_mondats);
	    $this->facture_mdl->attacher_bc_facture($id_bc,$id_fact);
			
		}else{
			echo('<script>alert("erreur bc ne exist pas")</script>');
		}
		}else{ // si le document est une convention
			$conv_existe=$this->convention_mdl->existe_conv($numero_piece,$id_fourn);
			
			if($conv_existe){
				$id_convention=$conv_existe[0]->id_convetion;
				
				$num_mond=$this->mondat_mdl->get_num_mondat();
		if($num_mond){
			$numero_mondat=$num_mond[0]->numero+1;
		}else{
			$numero_mondat=1;
		}
		$montant_facture=$fact_detail[0]->montant_fact;
		$mondat=$this->mondat_mdl->ajout_new_mondat($num_cheque,$montant_facture,$date,$numero_mondat);
		$id_mondats=$mondat[0]->id_mandat;
		$this->facture_mdl->attacher_mondat_facture($id_mondats,$id_fact);
		$this->convention_mdl->attacher_conv_mondat($id_convention,$id_mondats);
	    $this->facture_mdl->attacher_conv_facture($id_convention,$id_fact);
		
			}else{
				echo('<script>alert("erreur conv ne exist pas")</script>');
			}
		}
		$this->detail($id_fact);
	}
	
	
		public function ajout_new ()
	{
		$this->load->model('facture_mdl');
		
		///////////////////////	
		$nom_fournisseur= $this->input->post('fournisseur');
		$num_art= $this->input->post('article');
		$montant= $this->input->post('montant');
		//$mondat= $this->input->post('mondat');
		$numero= $this->input->post('numero'); 
	    $image=$this->input->post('image');
		$table['art']=$num_art;
		$table['montant']=$montant;
		$table['fournisseur']=$nom_fournisseur;
		$table['numero']=$numero;
		
		 $id_new=$this->facture_mdl->get_new_id();   // get all  for autocomplete	 
		 $id_fact=$id_new[0]->id_facture +1;
		 
		// chargement de image facture
	    $image_path='C:\Ampps\www\iqama_04_05_14\assets\telechargement';
	  	$config['upload_path'] = $image_path;
		$config['file_name']=$id_fact;
	 	$config['allowed_types'] = 'gif|jpg|png';
		$config['overwrite'] = TRUE;
		$this->load->library('upload', $config);
		$config['image_library'] = 'gd2';
	
		if ( ! $this->upload->do_upload())
		{
			$error = array('error' => $this->upload->display_errors());
			
		}
		else
		{
          
			$data = array('upload_data' => $this->upload->data());
	    
			$fpath=$image_path.'\\\\'.$data['upload_data']['file_name'];
			
		   $config['image_library'] = 'gd2';
		   $config['source_image'] = $fpath;
			$config['create_thumb'] = FALSE;
            $config['maintain_ratio'] = TRUE;
  		
			   $config['width'] = 200;
	           $config['height'] = 200;
		 
			    $this->load->library('image_lib', $config);
			    $this->image_lib->clear();
                $this->image_lib->initialize($config);
                $this->image_lib->resize();
				
				 
			
	
		$image=$config['file_name'].$data['upload_data']['file_ext'];

		}
		$this->load->model('facture_mdl'); 
		$this->facture_mdl->ajout_new($nom_fournisseur,$num_art,$montant,$numero,$image);
		
		
		 $this->load->library('session');
		$this->load->model('charticle_mdl'); // load the file welcome_mdl.php
		//$this->load->model('facture_mdl'); // load the file welcome_mdl.php
		$charticle = $this->charticle_mdl->get_all(); // call the method filtera inside the class welcome_mdl
		
		$data_charticle = array('data_charticle'=>$charticle); //ajouter la table r1 a data		
		$this->session->set_userdata($data_charticle); // mettre $data_charticle dans une var globale
		
		 $config = array();
          $config["base_url"] = base_url( "facture_ctr/index_test");
		  $config["total_rows"] =$this->facture_mdl->record_count_all();
		  $config["per_page"] = 20;
          $config["uri_segment"] = 3;
		  $config['first_link'] = 'PREMIER';
		  $config['last_link'] = 'DERNIER';
 

        $this->pagination->initialize($config);
		 $page = ($this->uri->segment(3)) ? $this->uri->segment(3) : 0;
       $data["results"] = $this->facture_mdl->get_all($config["per_page"], $page);
	   $data["results_recherche"] = $this->facture_mdl->get_all_nom();
        $data["links"] = $this->pagination->create_links();
		  $this->load->view('facture_vew',$data);  // send the result from the model to the view 
	}

      public function recherche_par_etat_paiement ()
	{
		$this->load->model('facture_mdl');
		
		$etat= $this->input->post('paiement');
		
		if($etat=='الكلية'){
			$this->index_test();
		}else{
			//$result=$this->facture_mdl->facture_non_payer();
			
			 $this->load->library('session');
		$this->load->model('charticle_mdl'); // load the file welcome_mdl.php
		$this->load->model('facture_mdl'); // load the file welcome_mdl.php
		$charticle = $this->charticle_mdl->get_all(); // call the method filtera inside the class welcome_mdl
		
		$data_charticle = array('data_charticle'=>$charticle); //ajouter la table r1 a data		
		$this->session->set_userdata($data_charticle); // mettre $data_charticle dans une var globale
		
		 $config = array();
          $config["base_url"] = base_url( "facture_ctr/recherche_par_etat_paiement");
		  $config["total_rows"] =$this->facture_mdl->record_count_all();
		  $config["per_page"] = 20;
          $config["uri_segment"] = 3;
		  $config['first_link'] = 'PREMIER';
		  $config['last_link'] = 'DERNIER';
 

        $this->pagination->initialize($config);
		 $page = ($this->uri->segment(3)) ? $this->uri->segment(3) : 0;
		  $data["titre"] = 'قائمة الفواتير غير مدفوعة';
       $data["results"] = $this->facture_mdl->facture_non_payer($config["per_page"], $page);
	   $data["results_recherche"] = $this->facture_mdl->get_all_nom();
        $data["links"] = $this->pagination->create_links();
		  $this->load->view('facture_vew',$data);  // send the result from the model to the view 
		}
				 
			
	
		
	}
	
	public function ajout_new1()
	{
		$this->load->model('facture_mdl');
		$this->load->model('fournisseur_mdl2');
		
		///////////////////////	
		$nom_fournisseur= $this->input->post('fournisseur');
		$type= $this->input->post('t');
		$numero_fact= $this->input->post('numero');
		
		if($nom_fournisseur==null or $numero_fact==NULL){
			echo('<script>alert("هناك حقل معبا بشكل خاطئ");</script>');
			$message=0;
			$this->new_facture($message);
		}else{
	
			
		$id_fourn=$this->fournisseur_mdl2->get_id($nom_fournisseur);
		$id_four=$id_fourn[0]->id_fournisseur;
		
		// on test si il existe une facture avec ce numero
		$tab_facts=$this->facture_mdl->existe_facture($numero_fact,$id_four);
		if($tab_facts)$existe_facture=true;
		else $existe_facture=false;
		
		if(!$existe_facture){
		
		 if($type=='bc'){
		 	$les_numero_document=$this->facture_mdl->get_numero_bc($id_four);
		 }elseif($type=='convention'){
		 	$les_numero_document=$this->facture_mdl->get_numero_conv($id_four);
		 }
		 
		 if($type=='autre'){
		 		 $data['numero_fact']=$numero_fact;
		 $data['id_fourn']=$id_four;
		 $data['nature']=$type;
		 $this->load->model('charticle_mdl');

		
		$data3['rec'] = $this->charticle_mdl->get_all();  //get all  list for autocomplete 
		$data['liste_article'] = $data3;
		
		  $this->load->view('facture_direct_vew',$data);  // send the result from the model to the view 
			
		 }else{
		 $data['numero']=$les_numero_document;
		 $data['numero_fact']=$numero_fact;
		 $data['id_fourn']=$id_four;
		 $data['nature']=$type;
		  $this->load->view('facture_choix_numero_vew',$data);  // send the result from the model to the view 
		  }
		  }else{
		  	$message=1;
			$this->new_facture($message);
		  }
		  
		  }
	}
	
	public function valider_ajout_facture ($id_fourn,$numero_fact,$nature)
	{
		$this->load->model('facture_mdl');
		
	
		$num= $this->input->post('tt');
		
		if($nature=='bc'){
	    $this->load->model('bc_mdl');
		$bc_produit=$this->bc_mdl->get_bc($id_fourn,$num);
		$montant=$bc_produit[0]->montant;
		/*foreach($bc_produit as $tmp){
			$montant=$montant+($tmp->quantite * $tmp->bc_produit_pu + ($tmp->quantite * $tmp->bc_produit_pu)/100*$tmp->tva);
		}*/
		
		
		}else{
			$this->load->model('convention_mdl');
			$convention_produit=$this->convention_mdl->get_convention($num,$id_fourn);
		    $montant=$convention_produit[0]->montant;
		/*foreach($convention_produit as $tmp){
			$montant=$montant+$tmp->quantite * $tmp->pu_convention_produit+($tmp->quantite * $tmp->pu_convention_produit/100*$tmp->tva);
		}	*/
		}
		
		if($montant==0){
			echo('<script>alert("لا يمكن تسديد فاتورة قيمتها 0 ");</script>');
			$message=0;
			$this->new_facture($message);
		}else{
			
		$this->facture_mdl->ajout_new_facture($id_fourn,$montant,$numero_fact);
		$tab_id=$this->facture_mdl->get_new_id();
		$id_fact=$tab_id[0]->id_facture;
		
		//creation mondat payement
		$this->load->model('mondat_mdl');
		
		$today = getdate();
		$date=$today['year'].'-'.$today['mon'].'-'.$today['mday'];
		
			$num_mond=$this->mondat_mdl->get_num_mondat();
		if($num_mond){
			$numero_mondat=$num_mond[0]->numero+1;
		}else{
			$numero_mondat=1;
		}
		$mondat=$this->mondat_mdl->ajout_new_mondat2($montant,$date,$numero_mondat);
		$id_mondats=$mondat[0]->id_mandat;
		$this->facture_mdl->attacher_mondat_facture($id_mondats,$id_fact);
		// chargement de image facture
				
	    $image_path='C:\Ampps\www\iqama_04_05_14\assets\telechargement';
	  	$config['upload_path'] = $image_path;
		$config['file_name']=$id_fact;
	 	$config['allowed_types'] = 'gif|jpg|png';
		$config['overwrite'] = TRUE;
		$this->load->library('upload', $config);
		$config['image_library'] = 'gd2';
	
		if ( ! $this->upload->do_upload())
		{
			$error = array('error' => $this->upload->display_errors());
			
		}
		else
		{
          
			$data = array('upload_data' => $this->upload->data());
	    
			$fpath=$image_path.'\\\\'.$data['upload_data']['file_name'];
			
		   $config['image_library'] = 'gd2';
		   $config['source_image'] = $fpath;
			$config['create_thumb'] = FALSE;
            $config['maintain_ratio'] = TRUE;
  		
			   $config['width'] = 200;
	           $config['height'] = 200;
		 
			    $this->load->library('image_lib', $config);
			    $this->image_lib->clear();
                $this->image_lib->initialize($config);
                $this->image_lib->resize();
				
				 
			
	
		$image=$config['file_name'].$data['upload_data']['file_ext'];
        $this->facture_mdl->ajout_image($id_fact,$image);
		}
		
		
		if($nature=='bc'){
			$id_bc=$bc_produit[0]->id_bc;
			$this->facture_mdl->attacher_bc_facture($id_bc,$id_fact);
		}else{
			$id_convention=$convention_produit[0]->id_convetion;
			$this->facture_mdl->attacher_conv_facture($id_convention,$id_fact);
		}
		$this->detail($id_fact);
		}
	}

    	public function valider_ajout_facture_direct ($id_fourn,$numero_fact,$nature)
	{
		$this->load->model('facture_mdl');
		$this->load->model('charticle_mdl');
		$this->load->model('engagement_mdl');
		
	
		$montant= $this->input->post('montant');
		$art= $this->input->post('article');
		
		if($art){
	      	$tab_art=$this->charticle_mdl->get_id_article($art);	
			$id_article=$tab_art[0]->id_article;	
		}
	
	
		if($montant==0){
			echo('<script>alert("لا يمكن تسديد فاتورة قيمتها 0 ");</script>');
			$message=0;
			$this->new_facture($message);
		}else{
				$num_enga=$this->engagement_mdl->get_engagement_prec($id_article);
		
		if(!$num_enga){
			 echo "<script> alert(\" non تمت عملية الالتزام بنجاح\")</script>";
			
		}else{
			if($num_enga[0]->nature==1){// si nature est depense
				$montant_prec=$num_enga[0]->montant_precedent-$num_enga[0]->montant_engagement;
			}else{
				$montant_prec=$num_enga[0]->montant_engagement+$num_enga[0]->montant_precedent;
			}
			}
			
			$numero=$num_enga[0]->numero+1;
			$nature=1;
			$today = getdate();
			$time=$today[0];
			$date=$today['year'].'-'.$today['mon'].'-'.$today['mday'];
			$this->engagement_mdl->ajout_engagement3($id_article,$nature,$date,$numero,$time,$montant,$montant_prec);
			$num_enga=$this->engagement_mdl->get_engagement_numero($id_article);
			$id_engagement=$num_enga[0]->id_engagement;
			
		$this->facture_mdl->ajout_new_facture_direct($id_fourn,$montant,$numero_fact,$id_engagement);
		$tab_id=$this->facture_mdl->get_new_id();
		$id_fact=$tab_id[0]->id_facture;
		
		//creation mondat payement
		$this->load->model('mondat_mdl');
		
		$today = getdate();
		$date=$today['year'].'-'.$today['mon'].'-'.$today['mday'];
		
			$num_mond=$this->mondat_mdl->get_num_mondat();
		if($num_mond){
			$numero_mondat=$num_mond[0]->numero+1;
		}else{
			$numero_mondat=1;
		}
		$mondat=$this->mondat_mdl->ajout_new_mondat2($montant,$date,$numero_mondat);
		$id_mondats=$mondat[0]->id_mandat;
		$this->facture_mdl->attacher_mondat_facture($id_mondats,$id_fact);
		// chargement de image facture
				
	    $image_path='C:\Ampps\www\iqama_04_05_14\assets\telechargement';
	  	$config['upload_path'] = $image_path;
		$config['file_name']=$id_fact;
	 	$config['allowed_types'] = 'gif|jpg|png';
		$config['overwrite'] = TRUE;
		$this->load->library('upload', $config);
		$config['image_library'] = 'gd2';
	
		if ( ! $this->upload->do_upload())
		{
			$error = array('error' => $this->upload->display_errors());
			
		}
		else
		{
          
			$data = array('upload_data' => $this->upload->data());
	    
			$fpath=$image_path.'\\\\'.$data['upload_data']['file_name'];
			
		   $config['image_library'] = 'gd2';
		   $config['source_image'] = $fpath;
			$config['create_thumb'] = FALSE;
            $config['maintain_ratio'] = TRUE;
  		
			   $config['width'] = 200;
	           $config['height'] = 200;
		 
			    $this->load->library('image_lib', $config);
			    $this->image_lib->clear();
                $this->image_lib->initialize($config);
                $this->image_lib->resize();
				
				 
			
	
		$image=$config['file_name'].$data['upload_data']['file_ext'];
        $this->facture_mdl->ajout_image($id_fact,$image);
		}
		
		
		
		$this->detail($id_fact);
		}
	}
	
		public function new_facture ($msg)
	{
		
		$this->load->model('charticle_mdl');
		$this->load->model('fournisseur_mdl2');
		
		$data2['rec'] = $this->fournisseur_mdl2->get_all();   // get all  for autocomplete
		$data3['rec'] = $this->charticle_mdl->get_all();  //get all  list for autocomplete 
		$r['msg']=$msg;
		$r['liste_fourn']    = $data2;
		$r['liste_article'] = $data3;
		if($msg==1)
	     echo'<script>alert("توجد فاتورة بهذا الرقم");</script>';
		$this->load->view('facture_new_vew',$r); 
	}
	
	public function ajout_image($tab){
		
		$this->load->model('facture_mdl'); 
		$this->facture_mdl->ajout_new($nom_fournisseur,$num_art,$montant,$mondat,$numero,$table,$image);
		 $this->load->view('facture_ajout_success_vew');
	}
	function do_upload()
	{
		$image_path='C:'.'\\\\'.'Users'.'\\\\SBI\\\\Desktop\\\\telechargement';
		$config['upload_path'] = $image_path;
		$config['allowed_types'] = 'gif|jpg|png';
		//$config['encrypt_name'] = FALSE;
		$config['overwrite'] = TRUE;
		$this->load->library('upload', $config);
		$config['image_library'] = 'gd2';
	
		if ( ! $this->upload->do_upload())
		{
			$error = array('error' => $this->upload->display_errors());

			$this->load->view('facture_upload_vew', $error);
		}
		else
		{
			 
			$data = array('upload_data' => $this->upload->data());
	
			$fpath=$image_path.'\\\\'.$data['upload_data']['file_name'];
		    $config['image_library'] = 'gd2';
			$config['source_image'] = $fpath;
			$config['create_thumb'] = FALSE;
            $config['maintain_ratio'] = TRUE;
  		
			   $config['width'] = 200;
	           $config['height'] = 200;
		 
			    $this->load->library('image_lib', $config);
			    $this->image_lib->clear();
                $this->image_lib->initialize($config);
                $this->image_lib->resize();
				$this->load->model('facture_mdl'); 
				 $this->facture_mdl->get_facture($fpath);
			$this->load->view('upload_success', $data);
			
			
		 
			   }
	}
}
?>